/*
 *  This file is part of LifeBudget - Copyright 2010 Clifton Eaton
 *  ------------------------------------------------------------------
 *  LifeBudget is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  LifeBudget is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with LifeBudget.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.lifebudget.app.client.mvc.view.paycheck;

import org.lifebudget.app.client.events.AppEvents;
import org.lifebudget.app.client.misc.NumberFormatter;
import org.lifebudget.app.client.mvc.model.PaycheckCalculator;
import org.lifebudget.app.client.mvc.model.PaycheckSummary;
import org.lifebudget.app.client.mvc.view.AppView;

import com.extjs.gxt.ui.client.Registry;
import com.extjs.gxt.ui.client.mvc.AppEvent;
import com.extjs.gxt.ui.client.mvc.Controller;
import com.extjs.gxt.ui.client.mvc.View;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Html;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;

/**
 * The Class PaycheckCalculatorSummaryView contains the view which shows
 * the current state of the {@link PaycheckCalculator} being edited in
 * the {@link PaycheckCalculatorEditorView}. This view is updated while editing
 * even before commiting {@link PaycheckCalculatorEditorView} changes.
 */
public class PaycheckCalculatorSummaryView extends View {

	/** The center. */
	private LayoutContainer center;

	/** The summary. */
	private PaycheckSummary summary;

	/** The remaining html. */
	private Html remainingHtml;

	/** The used html. */
	private Html usedHtml;

	/** The total html. */
	private Html totalHtml;

	/**
	 * Instantiates a new paycheck calculator summary view.
	 * 
	 * @param controller
	 *            the controller
	 */
	public PaycheckCalculatorSummaryView(Controller controller) {
		super(controller);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.extjs.gxt.ui.client.mvc.View#initialize()
	 */
	public void initialize() {
		summary = new PaycheckSummary();
	}

	/**
	 * Handles the <code>Init</code> event.
	 */
	public void initUI() {
		// get the reference to the center panel in the appview
		LayoutContainer summaryContainer = Registry.get(PaycheckCalculatorView.PAYCHECK_SUMMARY_CONTAINER);
		center = (LayoutContainer) Registry.get(AppView.CENTER_PANEL);

		summaryContainer.add(buildPaycheckSummary());
		center.layout();
	}

	/**
	 * Creates the html amounts.
	 */
	private void createHtmlAmounts() {
		totalHtml = new Html("<span class='categoryAmount'>"
				+ NumberFormatter.getCurrencyFormattedString(summary.getEstimatedAmt()) + "</span>");
		usedHtml = new Html("<span class='categoryAmount'>"
				+ NumberFormatter.getCurrencyFormattedString(summary.getUsedAmt()) + "</span>");
		remainingHtml = new Html("<span class='categoryAmount'>"
				+ NumberFormatter.getCurrencyFormattedString(summary.getRemainingAmt()) + "</span>");
	}

	/**
	 * Update html amounts.
	 */
	private void updateHtmlAmounts() {
		totalHtml.setHtml("<span class='categoryAmount'>"
				+ NumberFormatter.getCurrencyFormattedString(summary.getEstimatedAmt()) + "</span>");
		usedHtml.setHtml("<span class='categoryAmount'>"
				+ NumberFormatter.getCurrencyFormattedString(summary.getUsedAmt()) + "</span>");
		remainingHtml.setHtml("<span class='categoryAmount'>"
				+ NumberFormatter.getCurrencyFormattedString(summary.getRemainingAmt()) + "</span>");
	}

	/**
	 * Builds the paycheck summary.
	 * 
	 * @return the content panel
	 */
	private ContentPanel buildPaycheckSummary() {
		ContentPanel paycheckSummary = new ContentPanel();
		paycheckSummary.setHeading("Paycheck Summary");
		// paycheckSummary.setWidth(250);
		// total

		createHtmlAmounts();

		ContentPanel totalSummary = new ContentPanel();
		totalSummary.setHeading("Total");
		totalSummary.setLayout(new FitLayout());
		totalSummary.setFrame(true);

		totalSummary.add(totalHtml);

		// used
		ContentPanel usedSummary = new ContentPanel();
		usedSummary.setHeading("Used");
		usedSummary.setLayout(new FitLayout());
		usedSummary.setFrame(true);

		usedSummary.add(usedHtml);

		// remaining
		ContentPanel remainingSummary = new ContentPanel();
		remainingSummary.setHeading("Remaining");
		remainingSummary.setLayout(new FitLayout());
		remainingSummary.setFrame(true);

		remainingSummary.add(remainingHtml);

		paycheckSummary.add(totalSummary);
		paycheckSummary.add(usedSummary);
		paycheckSummary.add(remainingSummary);
		return paycheckSummary;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.extjs.gxt.ui.client.mvc.View#handleEvent(com.extjs.gxt.ui.client.
	 * mvc.AppEvent)
	 */
	@Override
	protected void handleEvent(AppEvent event) {
		if (event.getType() == AppEvents.Init) {
			initUI();
		}
		if (event.getType() == AppEvents.UpdateSummary) {
			updateSummary(event);
		}
		if (event.getType() == AppEvents.PaycheckCalcSelectionCleared) {
			resetSummary();
		}
	}

	/**
	 * Reset summary.
	 */
	private void resetSummary() {
		summary = new PaycheckSummary();
		updateHtmlAmounts();

	}

	/**
	 * Handles the <code>UpdateSummary</code> event.
	 * 
	 * @param event
	 *            the <code>UpdateSummary</code> event
	 */
	private void updateSummary(AppEvent event) {
		// GWT.log("Updating summary...");
		summary = (PaycheckSummary) event.getData();
		updateHtmlAmounts();
	}

}
